#include "oled_page_tool.h"
#define LOG_TAG     "oled_tool"     // 该模块对应的标签。不定义时，默认：NO_TAG
#define LOG_LVL     LOG_LVL_DBG   // 该模块对应的日志输出级别。不定义时，默认：调试级别
#include <ulog.h>                 // 必须在 LOG_TAG 与 LOG_LVL 下面
extern u8g2_t u8g2;


void oled_part_bh1750_u8g2_SendBuffer(void)
{
    //bh1750_close();
    u8g2_SendBuffer(&u8g2);
    //bh1750_open();
}


/*
 * 模型初始化界面绘制
 *
 * */
void oled_page_model_init(void)
{
    //font u8g2_font_fub17_tf Free
    //      init .....
    u8g2_uint_t  str_width=0;
    u8g2_uint_t  oled_width=0;

    u8g2_ClearBuffer(&u8g2);

    u8g2_SetFont(&u8g2,u8g2_font_maniac_tr);
    oled_width=u8g2_GetDisplayWidth(&u8g2);
    str_width=u8g2_GetStrWidth(&u8g2, "init .....");

    u8g2_DrawStr(&u8g2, (oled_width-str_width)/2, 45, "init .....");
    //u8g2_SendBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();
}

/*
 * 亮度调节界面，仅绘制
 */
void oled_page_light_control(void)
{
    char my_str[50];
    static rt_uint32_t last_percent=0;
    rt_uint32_t current_percent=0;
    current_percent=light_lx_user_dst_percent();

    if (last_percent!=current_percent) {
        u8g2_ClearBuffer(&u8g2);

        //太阳图标 font Pomodoro_wake_up_logo_font_name number Pomodoro_wake_up_logo_str_number_end
        u8g2_SetFont(&u8g2,Pomodoro_wake_up_logo_font_name);
        rt_sprintf(my_str, "%c",Pomodoro_wake_up_logo_str_number_end);
        u8g2_DrawStr(&u8g2, 8, 36, my_str);

        //百分比
        u8g2_SetFont(&u8g2,u8g2_font_fub17_tf);
        rt_sprintf(my_str, "%02d%%",current_percent);
        u8g2_DrawStr(&u8g2, 72, 36, my_str);
        //进度条
        u8g2_DrawFrame(&u8g2, 11, 44, 104, 17);
        u8g2_DrawBox(&u8g2, 13, 46, current_percent, 13);

        //u8g2_SendBuffer(&u8g2);
        oled_part_bh1750_u8g2_SendBuffer();
    }


    last_percent=current_percent;
}


struct {
    rt_uint16_t Pomodoro_ms;
    rt_uint16_t minute;
    rt_uint16_t sec;
    rt_uint8_t Pomodoro_state;//当前番茄钟的状态，0不需要显示，1需要显示
    char str[50];
    rt_uint8_t ring_sign;
    rt_uint8_t ring_sign_count;
}Pomodoro={
        .Pomodoro_ms=0,
        .minute=0,
        .sec=0,
        .ring_sign=0,
        .ring_sign_count=0,
        .Pomodoro_state=0
};

//番茄钟秒定时器回调函数
static void Pomodoro_timer_callback(void *parameter)
{
    if(Pomodoro.Pomodoro_ms>0)
    {
        Pomodoro.Pomodoro_ms--;
        Pomodoro.minute=Pomodoro.Pomodoro_ms/60;
        Pomodoro.sec=Pomodoro.Pomodoro_ms%60;
        rt_sprintf(Pomodoro.str, "%02d:%02d",Pomodoro.minute,Pomodoro.sec);
    }else {
        Pomodoro.ring_sign=1;//响铃
        Pomodoro.ring_sign_count++;
        Pomodoro.ring_sign_count%=2;
    }
}
//创建/复位番茄钟
void Pomodoro_create()
{
/*    rt_timer_t Pomodoro_timer_s=RT_NULL;

    Pomodoro_timer_s=(rt_timer_t)rt_object_find(Pomodoro_timer_name, RT_Object_Class_Timer);
    if (Pomodoro_timer_s==RT_NULL) {
        LOG_W("Pomodoro_timer_s find errror!");
        LOG_I("Pomodoro_timer_s create!");
        Pomodoro_timer_s= rt_timer_create(Pomodoro_timer_name,
                                        Pomodoro_timer_callback,
                                        RT_NULL,
                                        1000,
                                        RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER);
        if (Pomodoro_timer_s==RT_NULL)
            LOG_W("Pomodoro_timer_s create errror!");
    }
    Pomodoro.Pomodoro_ms=Pomodoro_ms_max;
    Pomodoro.minute=0;
    Pomodoro.sec=0;
    Pomodoro.ring_sign=0;
    Pomodoro.Pomodoro_state=1;*/
}

//开始番茄钟倒计时
void Pomodoro_start()
{
    rt_timer_t Pomodoro_timer_s=RT_NULL;
    Pomodoro_timer_s=(rt_timer_t)rt_object_find(Pomodoro_timer_name, RT_Object_Class_Timer);
    if (Pomodoro_timer_s==RT_NULL) {
        LOG_W("Pomodoro_timer_s find errror!");
        LOG_I("Pomodoro_timer_s create!");
        Pomodoro_timer_s= rt_timer_create(Pomodoro_timer_name,
                                        Pomodoro_timer_callback,
                                        RT_NULL,
                                        1000,
                                        RT_TIMER_FLAG_PERIODIC|RT_TIMER_FLAG_SOFT_TIMER);
        if (Pomodoro_timer_s==RT_NULL)
            LOG_W("Pomodoro_timer_s create errror!");
        Pomodoro.Pomodoro_ms=Pomodoro_ms_max;
        Pomodoro.ring_sign=0;

    }else {
        if (Pomodoro.Pomodoro_ms==0) {
            Pomodoro.Pomodoro_ms=Pomodoro_ms_max;
        } //防止停止之后开始导致计时清零

    }
    if (Pomodoro.Pomodoro_state==0) {//防止重复启动，导致定时器计数器清零
        rt_timer_start(Pomodoro_timer_s);//启动番茄钟倒计时软件定时器
    }
    Pomodoro.minute=Pomodoro.Pomodoro_ms/60;
    Pomodoro.sec=Pomodoro.Pomodoro_ms%60;
    rt_sprintf(Pomodoro.str, "%02d:%02d",Pomodoro.minute,Pomodoro.sec);
    Pomodoro.Pomodoro_state=1;

}
//暂停番茄钟倒计时//关闭
void Pomodoro_stop()
{
    rt_timer_t Pomodoro_timer_s=RT_NULL;
    Pomodoro_timer_s=(rt_timer_t)rt_object_find(Pomodoro_timer_name, RT_Object_Class_Timer);
    if (Pomodoro_timer_s!=RT_NULL) {
        rt_timer_stop(Pomodoro_timer_s);//停止番茄钟倒计时软件定时器
            rt_timer_delete(Pomodoro_timer_s);//清除番茄钟倒计时软件定时器
            Pomodoro.Pomodoro_ms=0;
            Pomodoro.minute=0;
            Pomodoro.sec=0;
            Pomodoro.ring_sign=0;
            Pomodoro.Pomodoro_state=0;

    }else {
        LOG_W("Pomodoro_timer_s find errror!");
    }


}

//清除番茄钟倒计时
void Pomodoro_clear()
{
/*    rt_timer_t Pomodoro_timer_s=RT_NULL;
    Pomodoro_timer_s=(rt_timer_t)rt_object_find(Pomodoro_timer_name, RT_Object_Class_Timer);
    if (Pomodoro_timer_s!=RT_NULL) {

        rt_timer_stop(Pomodoro_timer_s);//停止番茄钟倒计时软件定时器
        rt_timer_delete(Pomodoro_timer_s);//清除番茄钟倒计时软件定时器
        Pomodoro.Pomodoro_ms=0;
        Pomodoro.minute=0;
        Pomodoro.sec=0;
        Pomodoro.ring_sign=0;
        Pomodoro.Pomodoro_state=0;
    }else {
        LOG_W("Pomodoro_timer_s find errror!");
    }*/

}

rt_err_t Pomodoro_state_get(void)
{
    rt_err_t state=RT_EOK;

    if (Pomodoro.Pomodoro_state==0)
    {
        state=RT_ERROR;
    }
    return state;
}

/*
 * 番茄钟界面，仅绘制
 */

void oled_page_Pomodoro(void)
{
    char my_str[5];
    static rt_uint16_t last_ms_state=0;
    if (Pomodoro.ring_sign==0) {//未开始响铃，正常倒计时
        if (last_ms_state!=Pomodoro.Pomodoro_ms) {
            u8g2_ClearBuffer(&u8g2);
            //闹钟图标
            u8g2_SetFont(&u8g2,Pomodoro_logo_font_name);//u8g2_font_open_iconic_arrow_2x_t
            rt_sprintf(my_str, "%c",Pomodoro_logo_str_number);
            u8g2_DrawStr(&u8g2, 0, 40, my_str);
            //倒计时
            u8g2_SetFont(&u8g2, u8g2_font_logisoso30_tn);
            u8g2_DrawStr(&u8g2, 40, 40, Pomodoro.str);

            //已过去时间进度条
            u8g2_DrawFrame(&u8g2, 11, 44, 104, 17);
            u8g2_DrawBox(&u8g2, 13, 46, (u8g2_uint_t)(100-(((float)Pomodoro.Pomodoro_ms)*100/Pomodoro_ms_max)), 13);

            //u8g2_SendBuffer(&u8g2);
            oled_part_bh1750_u8g2_SendBuffer();
        }
        last_ms_state=Pomodoro.Pomodoro_ms;
    } else {//开始响铃借助软件定时器实现铃声图标闪烁
        static rt_uint8_t last_ring_sign_count=0;

        if (last_ring_sign_count!=Pomodoro.ring_sign_count) {
            u8g2_ClearBuffer(&u8g2);
            //闹钟图标
            if (Pomodoro.ring_sign_count) {
                u8g2_SetFont(&u8g2,Pomodoro_wake_up1_logo_font_name);//u8g2_font_open_iconic_arrow_2x_t
                rt_sprintf(my_str, "%c",Pomodoro_wake_up1_logo_str_number);
                u8g2_DrawStr(&u8g2, 48, 50, my_str);
            }

            //u8g2_SendBuffer(&u8g2);
            oled_part_bh1750_u8g2_SendBuffer();
        }
        last_ring_sign_count=Pomodoro.ring_sign_count;
    }

}

/*
 * 人来亮灯欢迎界面，仅绘制
 */
void oled_page_hello(void)
{
    u8g2_uint_t  str_width=0;
    u8g2_uint_t  oled_width=0;
    u8g2_ClearBuffer(&u8g2);

    u8g2_SetFont(&u8g2,u8g2_font_maniac_tr);//u8g2_font_open_iconic_arrow_2x_t
    oled_width=u8g2_GetDisplayWidth(&u8g2);
    str_width=u8g2_GetStrWidth(&u8g2, "HELLO");

    u8g2_DrawStr(&u8g2, (oled_width-str_width)/2, 45, "HELLO");

    //u8g2_SendBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();

    rt_thread_mdelay(1500);
    u8g2_ClearBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();
}


/*
 * 人走灭灯再见界面，仅绘制
 */
void oled_page_goodbye(void)
{
    u8g2_uint_t  str_width=0;
    u8g2_uint_t  oled_width=0;

    u8g2_ClearBuffer(&u8g2);

    u8g2_SetFont(&u8g2,u8g2_font_maniac_tr);//u8g2_font_open_iconic_arrow_2x_t
    oled_width=u8g2_GetDisplayWidth(&u8g2);
    str_width=u8g2_GetStrWidth(&u8g2, "goodbye");

    u8g2_DrawStr(&u8g2, (oled_width-str_width)/2, 45, "goodbye");

    //u8g2_SendBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();

    rt_thread_mdelay(1500);
    u8g2_ClearBuffer(&u8g2);
    oled_part_bh1750_u8g2_SendBuffer();
}



/*
 * 使用定时器动态操作
 * u8g2_SetClipWindow(&u8g2,10, 10, 85, 30);
 * 应该可以实现可打断动效（由点及面，由面到点）
 */
